#include "publicFunc.h"
#include <string.h>
#include <uuid/uuid.h>
#include <stdio.h>
#include <algorithm>

// 获取uuid
void GetUUID(char *cUUID, int size)
{
	memset(cUUID, 0, size);
	uuid_t uuid;
	uuid_generate(uuid);
	uuid_unparse(uuid, cUUID);

	string str(cUUID);
	str.erase(remove(str.begin(), str.end(), '-'));
	memset(cUUID, 0, size);
	strncpy(cUUID, str.c_str(), 32);
}

// 从beg开始往后取nCount个字符
string strMid(const char *src, int beg, int nCount)
{
    int len = strlen(src);
    if(src == NULL || beg >= len || nCount <= 0)
        return "";

    char buf[nCount+1];
    memset(buf, 0, sizeof(buf));
    int at = 0;
    for(int i = beg+1; i < len, nCount > 0; i++, nCount--)
    {
        buf[at] = src[i];
        at++;
    }
    
    //cout << buf << endl;
    string ret(buf);
    return ret;
}

// 截取从beg开始到结束的所有字符
string strMid(const char *src, int beg)
{
    int len = strlen(src);
    if(src == NULL || beg >= len)
        return "";
    //1234
    int ct = len - beg + 1;
    char buf[ct];
    memset(buf, 0, sizeof(buf));
    int n = 0;
    for(int i = beg+1; i < len; i++)
    {
        buf[n] = src[i];
        n++;
    }
    //cout << buf << endl;
    string ret(buf);

    return ret;
}

// 返回字符串左边n个字符
string StrLeft(string str, int nSize)
{
    size_t len = str.length();
    //cout << "len = " << len << endl;
    if(len <= 0 || nSize > len || nSize <= 0)
        return NULL;
    
    char *ret = new char[nSize+1];
    memset(ret, 0, nSize+1);
    for(size_t i = 0; i < nSize; i++)
    {
        ret[i] = str[i];
    }
    ret[nSize] = '\0';

    string strRet(ret);
    if(ret != NULL)
    {
        delete []ret;
        ret = NULL;
    }

    return strRet;
}

// 在Src中查找Sub
// 找到了返回最后一次出现的位置， 没找到返回-1
int strReverseFind(const char *strSrc, const char *strSub)
{
    if(strSrc == NULL || strSub == NULL)
		return -2;
    //0.0.0.0:*  :  8 1   7 0
	int len1 = strlen(strSrc);
	int len2 = strlen(strSub);
    if(len1 == 0 || len2 == 0)
        return -3;
    int index = len2-1;
    int ret = -1;
    int temp = -1;
    bool flag = true;
    bool flag2 = false;
    for(int i = len1-1; i >= 0; i--)
    {
        if(strSub[index] == strSrc[i])
        {
            if(index == 0)
                return i;
            index--;
            flag2 = true;
        }
        else
        {
            if(flag2)
                i++;
            flag2 = false;
            index = len2-1;
        }
    }
    return -1;
}

// 在Src中查找Sub
// 找到了返回首次出现的位置， 没找到返回-1
int strFind(const char *strSrc, const char *strSub)
{
	if(strSrc == NULL || strSub == NULL)
		return -2;
	int len1 = strlen(strSrc);
	int len2 = strlen(strSub);
    if(len1 == 0 || len2 == 0)
        return -3;
	int index = 0;
    int ret = -1;
    bool flag = true;
	for(int i = 0; i < len1 ; i++)
	{
		if(strSrc[i] == strSub[index] && index < len2)
        {
            if(flag)
            {
                ret = i;
                flag = false;
            }
			index++;
        }
		else
		{
			if(index == len2)
				return ret;
			else
			{
                flag = true;
				index = 0;
			}
		}	
	}

	return -1;
}

// 替换字符串中的指定字符
// str : 需要替换的字符串
// cOld: 被替换的字符（可以是字符串）
// cNew: 新字符（可以是字符串）
// 注：原字符串地址空间必须够存替换后的字符串，否则会越界
int StrReplace(char *str, char *cOld, char *cNew)
{
	if(str == NULL || cOld == NULL)
		return -1;
	
	int nSize1 = strlen(str);
	int nSize2 = strlen(cOld);
	int nSize3 = strlen(cNew);

	//char StrTemp[1024] = {0};
	char *StrTemp = new char[nSize1+128];
	if(StrTemp == NULL)
	{
		perror("new failed");
		return -2;
	}

	//memset(StrTemp, 0, sizeof(char)*1024);
	memset(StrTemp, 0, sizeof(char)*(nSize1+128));
	int x = 0;
	int y = 0;
	for(int i = 0; i < nSize1;)
	{
		if(strncmp(str+i, cOld, nSize2) == 0)
		{
			//cout << "有" << endl;
			for(int j = 0; j < nSize3; j++)
			{
				StrTemp[y+j] = cNew[j];
			}
			i += nSize2;
			x += nSize2;
			y += nSize3;
		}
		else
		{
			StrTemp[y] = str[x];
			i++;
			x++;
			y++;
		}
	}

	StrTemp[y] = '\0';
	strcpy(str, StrTemp);

	delete []StrTemp;
   
	return 0;
}

int StrReplace(string&src, char *cOld, char *cNew)
{
	if(src.empty() || cOld == NULL)
		return -1;
	
	int nSize1 = src.size();
	int nSize2 = strlen(cOld);
	int nSize3 = strlen(cNew);

	//char StrTemp[1024] = {0};
	char *StrTemp = new char[nSize1+128];
	if(StrTemp == NULL)
	{
		perror("new failed");
		return -2;
	}

	char *cSrc = (char *)src.c_str();

	//memset(StrTemp, 0, sizeof(char)*1024);
	memset(StrTemp, 0, sizeof(char)*(nSize1+128));
	int x = 0;
	int y = 0;
	for(int i = 0; i < nSize1;)
	{
		if(strncmp(cSrc+i, cOld, nSize2) == 0)
		{
			//cout << "有" << endl;
			for(int j = 0; j < nSize3; j++)
			{
				StrTemp[y+j] = cNew[j];
			}
			i += nSize2;
			x += nSize2;
			y += nSize3;
		}
		else
		{
			StrTemp[y] = cSrc[x];
			i++;
			x++;
			y++;
		}
	}

	StrTemp[y] = '\0';
	//strcpy(str, StrTemp);
	src = StrTemp;
	delete []StrTemp;

	return 0;
}

